<template>
  <div class="main-div">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getRoleList">查询</el-button>
      </el-form-item>
    </el-form>

    <div class="add-div">
      <el-button type="primary" plain @click="show">新增</el-button>
    </div>

    <el-table :data="roleList" style="width: 80%">
      <el-table-column prop="id" label="id" width="180" />
      <el-table-column prop="roleName" label="角色名称" width="180" />
      <el-table-column prop="menuNames" label="角色对应权限" width="180" />
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button link type="primary" size="small" @click="handlerUpdate(row)">修改</el-button>
          <el-button link type="primary" size="small" @click="shouquan(row)">授权</el-button>
          <el-button link type="primary" size="small" @click="deleteRole(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="showPagination">
      <el-pagination :page-size="20" :pager-count="11" layout="prev, pager, next" :total="1000" />
    </div>

    <el-dialog width="30%" :modal="roleInfo" v-model="dialogVisible">
      <el-form ref="formTemp" label-width="100px">
        <el-form-item label="角色名称">
          <el-input v-model="roleInfo.roleName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button type="primary" @click="handlerClose()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog width="30%" :modal="roleMenuInfo" v-model="dialogVisibleShouquan">
      <el-form ref="formTemp" label-width="100px">
        <div class="m-4">
          <el-cascader v-model="casValue"  :options="menuList" :props="{value: 'id', label: 'menuName', children: 'children', multiple: true, checkStrictly: true }" 
           clearable placeholder="请选择菜单" collapse-tags expandTrigger="hover" @change="change"/>
        </div>
        <el-form-item>
          <el-button type="primary" @click="onSubmit1">提交</el-button>
          <el-button type="primary" @click="handlerClose()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import {RoleList, RoleAdd, RoleDelete, RoleMenuAdd, MenuFirstListAuthor, RoleUpdate, MenuAllList} from '@/api/admin.js'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
  import { GetMenuShouquanList} from '@/api/admin.js'

const router = useRouter();

var dialogVisible = ref(false);
var dialogVisibleShouquan = ref(false);

var roleName = ref();

var roleList = ref([]);

var roleId = ref(0);

var menuList = ref([]);

var showPagination = ref(false);

var tag = ref(-1); 

var casValue = ref([]);

 
var roleInfo = ref({
  menuName: ''
});

var roleMenuInfo = ref({
  roleId: 0,
  menuIds:''
});


getRoleList();

function getRoleList() {

  RoleList({roleName:roleName.value})
    .then(res => {
      roleList.value = res.data.data;
      if (res.data.data.totalPage > 0) {
        showPagination = true
      }
    })
}

function getMenuFirstListAuthor(data){
  MenuFirstListAuthor(data)
    .then(res => {
      casValue.value = res.data.data;
    })
}

const onSubmit = () => {
  if (roleInfo.value.id == null){
    RoleAdd(roleInfo.value)
        .then(res => {
          if (res.data.code == 200000) {
            hide();
            getRoleList();
          } else {
            //错误提示框
            ElMessage({
              message: res.data.errorMessage,
              type: 'warning'
            });
          }
        })
  }else {
    RoleUpdate(roleInfo.value)
        .then(res => {
          if (res.data.code == 200000) {
            hide();
            getRoleList();
          } else {
            //错误提示框
            ElMessage({
              message: res.data.errorMessage,
              type: 'warning'
            });
          }
        })
  }

}
const onSubmit1 = () => {
  roleMenuInfo.value.roleId=roleId.value;
  roleMenuInfo.value.menuIds=JSON.stringify(casValue.value);
  console.info( roleMenuInfo.value);
  RoleMenuAdd(roleMenuInfo.value)
    .then(res => {
      if (res.data.code == 200000) {
        dialogVisibleShouquan.value = false;
        getRoleList();
      } else {
        //错误提示框
        ElMessage({
          message: res.data.errorMessage,
          type: 'warning'
        });
      }
    })
}
function deleteRole(data) {
  ElMessageBox.confirm(
    '是否确认删除',
    '提醒', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }
  ).then(() => {
    RoleDelete(data.id)
      .then(res => {
        if (res.data.code == 200000) {
          hide();
          getRoleList();
          ElMessage({
            type: 'success',
            message: '删除成功'
          });
        } else {
          ElMessage({
            message: res.data.errorMessage,
            type: 'warning'
          });
        }
      })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '删除取消'
    })
  })
}
function show() {
  dialogVisible.value = true;
  getRoleList();
}
function hide() {
  dialogVisible.value = false;
}

function shouquan(data){
  roleId=ref(0);
  getMenuFirstListAuthor(data.id);
  dialogVisibleShouquan.value = true;
  roleId.value=data.id;
  getMenuList();
}

function shouquanClose(){
  dialogVisibleShouquan.value = true;
}
  
function getMenuList() {
  MenuAllList()
      .then(res => {
        menuList.value = res.data.data;
      })
  }



  function change(item){ 
      item.forEach((i) => {
          console.info(i);
          if (i[0] != tag.value) {
            tag.value = i[0];
          }
      }); // 如切换勾选，则让标识符的值等于勾选的父级的value
    let filterd = item.filter((v) => v[0] == tag.value); // 过滤出与标识符相符的选项
    
    // casValue.value = [];
    casValue.value.push(...filterd);
  }
function handlerUpdate(role){
  roleInfo.value = role;
  dialogVisible.value=true
}

function handlerClose(){
  roleInfo.value={}
  dialogVisibleShouquan.value=false;

  dialogVisible.value=false;
  roleMenuInfo.value={}
}
</script>
<style>
body {
  background-color: aliceblue;
}

.main-div {
  border: 1px solid #cccccc;
  border-radius: 5px;
  background-color: white;
}

.el-form {
  margin-top: 8px;
  padding-left: 18px;
  background-color: white;
}

.add-div {
  padding-left: 18px;
}

.el-table {
  margin-top: 5px;
  padding-left: 10px;
  /* border-radius: 5% 5% 5% 5%;  */
  /* transform: skew(10deg); */
}

.el-table__column-resize-proxy {
  display: block;
}
</style>